<%--
  Created by IntelliJ IDEA.
  User: WangWeiwei
  Date: 16-10-22
  Time: 下午3:27
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
            + path;
    request.setAttribute("basePath", basePath);
%>
<div class="top-grids">
    <div style="margin: 50px;"></div>
    <h3>${web['title']}
        <button id="pushNewGoods" class="btn btn-success">发布商品</button>
    </h3>
    <div>
        <div class="panel panel-default" id="conditions-for-screening-panel0">
            <div class="panel-heading">展示图片</div>

            <div class="panel-body">
                <button id="btn-upload-picture" class="btn btn-primary">上传图片</button>
                <div type="text/plain" id="upload-picture" class="span12" style="height: 200px;">
                </div>
                <script>
                    var $uploadPicture;
                    $(function () {
                        var uploadPicture = UM.getEditor("upload-picture",{toolbar:['image']});
                        uploadPicture.setHide();
                        $uploadPicture = uploadPicture;
                        $("#btn-upload-picture").click(function () {
                            uploadPicture.show();
                            $("#conditions-for-screening-panel0").find("div[data-original-title='图片']").click();

                        });
                    })
                </script>
            </div>
        </div>
        <div class="panel panel-default" id="conditions-for-screening-panel1">
            <div class="panel-heading">基本属性</div>
            <div class="panel-body">
                <%@include file="add_goods_select.jsp"%>
            </div>
        </div>
        <div class="panel panel-default" id="conditions-for-screening-panel2">
            <div class="panel-heading">更多信息</div>
            <div class="panel-body">

                <div>
                    <%--ue编辑器--%>
                        <%--编辑器--%>
                    <script>
                        //1.设置编辑器配置文件的根路径
                        window.UMEDITOR_HOME_URL = '${basePath}/resources/umeditor/';
                    </script>
                    <%@include file="../../utils/umeditor.jsp"%>
                    <script>

                        $(function () {
                            //2.初始化编辑器的内容
                            um${editorId}.setContent('在这里编辑商品的详情信息');
                        })
                    </script>

                    <script id="my"></script>
                </div>
            </div>
        </div>

    </div>
</div>
<script>
    /**计算商品类型id*/
    var getGoodsTypeId = function (goods_type1,goods_type2) {
        var jsonStr = "\"";
        if (goods_type2 == 0){
            goods_type1 == 0 ?  jsonStr += "0": jsonStr += goods_type1;
        }else {
            jsonStr += goods_type2;
        }
        jsonStr += "\""
        return jsonStr;
    };
    /**计算商品适用作物或适用症状*/
    var getBehhoof = function (goods_crops,type) {
        var jsonStr = "";
        for (var i in goods_crops){
            if (type == 1){
                jsonStr += "\"crops[" ;
            }else if(type == 2){
                jsonStr += "\"symptons[" ;
            }
            jsonStr += i + "].id\":\"" + goods_crops[i] + "\",";
        }
        return jsonStr;
    };
    $(function(){
        function jsonStringToObj(goods_pictures, goods_name, goods_type1, goods_type2, goods_crops, goods_symptons, goods_standards, goods_code, goods_productor, goods_describe) {
            var string = "{\"pictureHtml\":\"\"," +
                    "\"goodsName\":\"" + goods_name +"\"," +
                    "\"type.id\":" +  getGoodsTypeId(goods_type1, goods_type2) + ","+
                    getBehhoof(goods_crops,1) +
                    getBehhoof(goods_symptons,2)+
                    "\"standard\" : \"" + goods_standards + "\"," +
                    "\"code\" :\"" + goods_code +"\","+
                    "\"productor.name\":\"" + goods_productor + "\","+
                    "\"describe\" :\"\"}";
            var obj = JSON.parse(string);
            obj.pictureHtml = goods_pictures;
            obj.describe = goods_describe;
            return obj;
        }
        function getGoodsMessage() {
            //商品图片
            var goods_pictures = $uploadPicture.getContent();
            //商品名
            var goods_name = $("#add-goods-goods-name").val();
            //商品类型1
            var goods_type1 = $("#add-goods-select-goods-type1").val();
            //商品类型2
            var goods_type2 = $("#add-goods-select-goods-type2").val();
            //作用作物 其值的实例 ["-1", "5", "4"]
            var goods_crops = $("#add-goods-crops").val();
            //适用症状
            var goods_symptons = $("#add-goods-sympton").val();
            //商品规格
            var goods_standards = $("input[placeholder='规格']").val();
            //商品条码
            var goods_code = $("input[placeholder='条码']").val();
            //生产厂商
            var goods_productor = $("#add-goods-productor").val();
            //商品详情信息
            var goods_describe = um.getContent();
            //发布商品参数
            var param1 = jsonStringToObj(goods_pictures, goods_name, goods_type1, goods_type2, goods_crops, goods_symptons, goods_standards, goods_code, goods_productor, goods_describe)
//            console.log(param1);
            param = param1;
        }
        var param;
        $("#pushNewGoods").click(function () {
            getGoodsMessage();
//            console.log(param);
            $.post("${basePath}/manager/goods/push_new_goods",param,function (data) {
                if(data == 1){}
                else{
                    //商品发布失败,给用户提示
                }
            })
        })
    })
</script>